// 添加智能锁
import debounce from "debounce";
import * as LockAPI from "../../api/interfaces/lock";
import { HttpHandler } from "../../api/handle/httpHandler";
import * as MSroomAPI from "../../api/interfaces/MSroom";
import * as EKeyAPI from "../../api/interfaces/key";

Page({
  data: {
    lockList: new Array<TTLockFromScan>(), // 蓝牙扫描锁列表
    state: '', // 错误提示
    isInitLock: false, // 是否正在初始化智能锁
    roomId: 0
  },
  onLoad(opt) {
    this.roomId = opt.roomId;


  },
  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {
    this.stopScan();
  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {
    this.stopScan();
  },

  // 开始扫描附近的智能锁设备
  startScan() {

    this.data.lockList.splice(0, this.data.lockList.length);
    
    this.setData({ lockList: this.data.lockList, state: "启动蓝牙设备扫描" });

    requirePlugin("myPlugin", ({ startScanBleDevice }: TTLockPlugin) => {
      // 开启蓝牙设备扫描
      startScanBleDevice((lockDevice, lockList) => {

        // TODO 成功扫描到设备
        if (!this.data.isInitLock) {
          this.setData({ lockList: lockList, state: "蓝牙设备扫描中" });
    console.log(this.data.lockList,'lockList');

        }


      }, (err) => {
        HttpHandler.showErrorMsg(err.errorMsg);
        this.data.lockList.splice(0, this.data.lockList.length);
        this.setData({ lockList: this.data.lockList, state: `蓝牙扫描开启失败：${err.errorMsg}` });
        
      });
    });
  },

  // 停止蓝牙扫描设备
  stopScan() {
    this.setData({ state: "正在停止搜索蓝牙设备" });
    requirePlugin("myPlugin", ({ stopScanBleDevice }: TTLockPlugin) => {
      // 关闭蓝牙设备扫描
      stopScanBleDevice().then(res => {
        // TODO 关闭蓝牙设备扫描返回
        this.data.lockList.splice(0, this.data.lockList.length);
        this.setData({
          lockList: this.data.lockList,
          state: res.errorCode == 0 ? "蓝牙设备扫描已关闭" : `关闭蓝牙扫描失败：${res.errorMsg}`
        });
      });
    });
  },
  /* 更新电子钥匙列表 */
  modifyKeyList: debounce(function (lockId) {
    EKeyAPI.list({
      pageNo: 1,
      pageSize: 20
    }).then(res => {
      if (HttpHandler.isResponseTrue(res)) {
        console.log(res.result);
        // requirePlugin("myPlugin", ({ resetLock }: TTLockPlugin) => {
        //   resetLock({ lockData: lockData }).then(res => {
        //     console.log(res.errCode);

        //     // if (res.errorCode == 0) {
        //     // 	this.setData({ state: `智能锁已重置, 操作时间：${Date.now() - start}ms.` });
        //     // 	// 同步到服务器
        //     // 	wx.showLoading({ title: "正在同步服务器" });
        //     // 	LockAPI.Delete({
        //     // 		lockId: this.data.keyInfo.lockId
        //     // 	}).then(res => {
        //     // 		if (HttpHandler.isResponseTrue(res)) {
        //     // 			wx.hideLoading();
        //     // 			this.setData({ state: "智能锁已删除" });
        //     // 			HttpHandler.showErrorMsg("智能锁已删除");
        //     // 			setTimeout(wx.navigateBack, 2000);
        //     // 		} else {
        //     // 			wx.hideLoading();
        //     // 			HttpHandler.handleResponseError(res);
        //     // 			this.setData({ state: `同步服务器失败，智能锁已重置` });
        //     // 		}
        //     // 	}).catch(err => {
        //     // 		wx.hideLoading();
        //     // 		HttpHandler.handleServerError(err);
        //     // 	})
        //     // } else {
        //     // 	wx.hideLoading();
        //     // 	HttpHandler.showErrorMsg("重置智能锁失败");
        //     // 	this.setData({ state: `重置智能锁失败: ${res.errorMsg}` });
        //     // }
        //   })
        // });
      }
    })
  }, 100),
  // 停止所有蓝牙操作，并退出操作中状态
  handleStopAllOperations() {
    this.setData({ state: "正在停止全部蓝牙操作" });
    requirePlugin("myPlugin", ({ stopAllOperations }: TTLockPlugin) => {
      // 停止所有蓝牙操作
      stopAllOperations().then(res => {
        // TODO 停止蓝牙操作返回
        HttpHandler.showErrorMsg(res.errorMsg);
        this.data.lockList.splice(0, this.data.lockList.length);
        this.setData({
          lockList: this.data.lockList,
          state: res.errorCode == 0 ? "蓝牙操作已关闭" : `停止蓝牙操作失败：该接口无法打断正在连接的动作`
        });
      });
    });
  },

  // 初始化蓝牙设备
  init(event) {
    this.data.isInitLock = true;
    const index = event.currentTarget.dataset.index;
    const lockItem = this.data.lockList[index];
    this.handleInitLock(lockItem);
  },

  // 初始化智能锁
  handleInitLock: debounce(function (deviceFromScan: TTLockFromScan) {
    let that = this;
console.log(deviceFromScan,'deviceFromScan');

    if (!deviceFromScan.isSettingMode) {
      this.setData({ state: `请重置` });
      MSroomAPI.getRoomInfoByLockId({
        lockId: deviceFromScan.deviceName
      }).then(res => {
        this.setData({ state: `确认` });
        if (res.result == null) {
          this.setData({ lockList: this.data.lockList, state: `正在初始化蓝牙智能锁${deviceFromScan.deviceName}, MAC地址：${deviceFromScan.MAC}` });
          requirePlugin("myPlugin", ({ getLockVersion, initLock }: TTLockPlugin) => {
            // 更新智能锁版本信息
            getLockVersion({ deviceFromScan }).then(res => {
              if (res.errorCode == 0) {
                // TODO 更新版本信息成功
                this.setData({ state: "智能锁版本信息已更新，正在初始化设备" });
                // 调用添加锁接口, serverTime传入服务器时间
                initLock({ deviceFromScan }).then(result => {
                  if (result.errorCode == 0) {
                    // 设备已成功初始化，请调用开放平台接口上传lockData
                    this.setData({ state: "设备已成功初始化，正在调用开放平台接口上传锁数据" });
                    LockAPI.initialize({ lockData: result.lockData }).then(res => {
                      if (HttpHandler.isResponseTrue(res)) {
                        HttpHandler.showErrorMsg("智能锁已添加");
                        MSroomAPI.bindRoomWithLock({
                          roomId: this.roomId,
                          lockInfo: deviceFromScan.deviceName,
                        }).then(res => {
                          this.setData({ state: "设备已同步房间" });
                          this.setData({ isInitLock: false });
    
                          // setTimeout(wx.navigateBack, 2000);
    
                        })
    
                      } else {
                        HttpHandler.handleResponseError(res);
                        // this.setData({ state: "智能锁数据上传失败, 正在重置智能锁" });
                        this.handleResetLock(result.lockData);
                      }
                    }).catch(err => {
                      HttpHandler.handleServerError(err);
                      // this.setData({ state: "智能锁数据上传失败, 正在重置智能锁" });
                      this.handleResetLock(result.lockData);
                    })
                  }
                  // else this.setData({ isInitLock: false, state: `初始化智能锁失败：${result.errorMsg}` });
                })
              }
              // else this.setData({ isInitLock: false, state: `更新智能锁版本信息失败：${res.errorMsg}` });
            });
          });
          // EKeyAPI.list({
          //   pageNo: 1,
          //   pageSize: 20
          // }).then(res => {
          //   if (HttpHandler.isResponseTrue(res)) {
          //     res.list.forEach(e => {
          //       if (e.lockName == deviceFromScan.deviceName) {
          //         requirePlugin("myPlugin", ({ resetLock }: TTLockPlugin) => {
          //           resetLock({ lockData: e.lockData }).then(res => {
          //             if (res.errorCode == 0) {
          //               that.setData({ state: `智能锁已重置` });
          //               // 同步到服务器
          //               wx.showLoading({ title: "正在同步服务器" });
          //               LockAPI.Delete({
          //                 lockId: e.lockId
          //               }).then(res => {
          //                 if (HttpHandler.isResponseTrue(res)) {
          //                   wx.hideLoading();
          //                   that.setData({ lockList: [], state: "请唤醒设备,手动绑定" });
          //                   that.setData({ isInitLock: false });
          //                   HttpHandler.showErrorMsg("智能锁已删除");
          //                 } else {
          //                   wx.hideLoading();
          //                   HttpHandler.handleResponseError(res);
          //                   that.setData({ state: `同步服务器失败，智能锁已重置` });
          //                 }
          //               }).catch(err => {
          //                 wx.hideLoading();
          //                 HttpHandler.handleServerError(err);
          //               })
          //             } else {
          //               wx.hideLoading();
          //               HttpHandler.showErrorMsg("重置智能锁失败");
          //               that.setData({ state: `重置智能锁失败: ${res.errorMsg}` });
          //             }
          //           })
          //         });
          //       }
          //     })
          //   }
          // })

         } else {
          wx.showModal({
            title: '提示',
            content: '已经绑定' + res.result.roomNo + '房间，是否删除绑定？',
            success(res) {
              if (res.confirm) {
                that.setData({ state: `用户确认` });
                EKeyAPI.list({
                  pageNo: 1,
                  pageSize: 20
                }).then(res => {
                  if (HttpHandler.isResponseTrue(res)) {
                    res.list.forEach(e => {
                      if (e.lockName == deviceFromScan.deviceName) {
                        requirePlugin("myPlugin", ({ resetLock }: TTLockPlugin) => {
                          resetLock({ lockData: e.lockData }).then(res => {
                            if (res.errorCode == 0) {
                              that.setData({ state: `智能锁已重置` });
                              // 同步到服务器
                              wx.showLoading({ title: "正在同步服务器" });
                              LockAPI.Delete({
                                lockId: e.lockId
                              }).then(res => {
                                if (HttpHandler.isResponseTrue(res)) {
                                  wx.hideLoading();
                                  that.setData({ lockList: [], state: "请唤醒设备,手动绑定" });
                                  that.setData({ isInitLock: false });
                                  HttpHandler.showErrorMsg("智能锁已删除");
                                } else {
                                  wx.hideLoading();
                                  HttpHandler.handleResponseError(res);
                                  that.setData({ state: `同步服务器失败，智能锁已重置` });
                                }
                              }).catch(err => {
                                wx.hideLoading();
                                HttpHandler.handleServerError(err);
                              })
                            } else {
                              wx.hideLoading();
                              HttpHandler.showErrorMsg("重置智能锁失败");
                              that.setData({ state: `重置智能锁失败: ${res.errorMsg}` });
                            }
                          })
                        });
                      }
                    })
                  }
                })
              } else if (res.cancel) {
                that.setData({ state: `用户取消` });
                console.log('用户点击取消')
              }
            }
          })
        }
      })
      return HttpHandler.showErrorMsg(`智能锁${deviceFromScan.deviceName || deviceFromScan.MAC}已被初始化，当前不可添加`);
    } else {
      this.data.lockList.splice(0, this.data.lockList.length);
      this.setData({ lockList: this.data.lockList, state: `正在初始化蓝牙智能锁${deviceFromScan.deviceName}, MAC地址：${deviceFromScan.MAC}` });
      requirePlugin("myPlugin", ({ getLockVersion, initLock }: TTLockPlugin) => {
        // 更新智能锁版本信息
        getLockVersion({ deviceFromScan }).then(res => {
          if (res.errorCode == 0) {
            // TODO 更新版本信息成功
            this.setData({ state: "智能锁版本信息已更新，正在初始化设备" });
            // 调用添加锁接口, serverTime传入服务器时间
            initLock({ deviceFromScan }).then(result => {
              if (result.errorCode == 0) {
                // 设备已成功初始化，请调用开放平台接口上传lockData
                this.setData({ state: "设备已成功初始化，正在调用开放平台接口上传锁数据" });
                LockAPI.initialize({ lockData: result.lockData }).then(res => {
                  if (HttpHandler.isResponseTrue(res)) {
                    HttpHandler.showErrorMsg("智能锁已添加");
                    MSroomAPI.bindRoomWithLock({
                      roomId: this.roomId,
                      lockInfo: deviceFromScan.deviceName,
                    }).then(res => {
                      this.setData({ state: "设备已同步房间" });
                      this.setData({ isInitLock: false });

                      // setTimeout(wx.navigateBack, 2000);

                    })

                  } else {
                    HttpHandler.handleResponseError(res);
                    // this.setData({ state: "智能锁数据上传失败, 正在重置智能锁" });
                    this.handleResetLock(result.lockData);
                  }
                }).catch(err => {
                  HttpHandler.handleServerError(err);
                  // this.setData({ state: "智能锁数据上传失败, 正在重置智能锁" });
                  this.handleResetLock(result.lockData);
                })
              }
              // else this.setData({ isInitLock: false, state: `初始化智能锁失败：${result.errorMsg}` });
            })
          }
          // else this.setData({ isInitLock: false, state: `更新智能锁版本信息失败：${res.errorMsg}` });
        });
      });
    }

  }, 100),






  // 重置智能锁
  handleResetLock: debounce(function (lockData: string) {
    requirePlugin("myPlugin", ({ resetLock }: TTLockPlugin) => {
      // 重置智能锁
      resetLock({ lockData }).then(res => {
        if (res.errorCode == 0) this.setData({ isInitLock: false, state: "初始化智能锁失败，智能锁已重置" });
        else this.setData({ isInitLock: false, state: `智能锁重置失败，请长按重置键进行设备重置：${res.errorMsg}` });
      });
    });
  }, 100),
})